package com.idanchuang.component.consumer;

import com.idanchuang.component.consumer.config.RibbonResolveMapConfig;
import com.idanchuang.component.consumer.ribbon.DirectResolveAroundRule;
import com.idanchuang.component.consumer.ribbon.DirectResolveRule;
import com.idanchuang.component.consumer.ribbon.lb.AcgRibbonLoadBalancedRetryFactory;
import com.idanchuang.sun.client.socket.discovery.AroundRule;
import com.netflix.loadbalancer.IRule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * @author yjy
 * @date 2019/12/26 9:30
 **/
@Configuration
public class ConsumerRibbonConfiguration {

    @Bean
    @ConfigurationProperties("idanchuang.component.consumer.ribbon")
    public RibbonResolveMapConfig ribbonResolveMapConfig() {
        return new RibbonResolveMapConfig();
    }

    @Bean
    public AroundRule directResolveAroundRule(RibbonResolveMapConfig ribbonResolveMapConfig) {
        return new DirectResolveAroundRule(ribbonResolveMapConfig);
    }

    @Bean
    @Scope(SCOPE_PROTOTYPE)
    public IRule directResolveRule(RibbonResolveMapConfig ribbonResolveMapConfig) {
        return new DirectResolveRule(ribbonResolveMapConfig);
    }

    @Bean
    public LoadBalancedRetryFactory loadBalancedRetryPolicyFactory(
            final SpringClientFactory clientFactory) {
        return new AcgRibbonLoadBalancedRetryFactory(clientFactory);
    }

}
